Repository navigation
[1402] OAuth 凭据收到 401 后强制刷新重试一次 - #15
Merged
Merged
Conversation
服务端可能在本地 expires 之前判 access token 失效返回 401, 1402 仅在请求前用 expires 判断刷新,请求层无 401 回退,直接 HTTP 401 stream error 失败。本任务仿照 pi-rotate-api-key! 钩子 范式新增 401 后 oauth 强制刷新回退: - keyring: 新增 pi-oauth-refresh-enabled? / pi-refresh-oauth-key! - resolve: 导出 pi-force-refresh-oauth-credential-key(不查 expires) 与 pi-has-oauth-credential? - config: provider 有 oauth 凭据时注入 refresh-oauth-key 闭包钩子 - 请求层 stream/request: cond 新增 401 分支,配 refreshed-once? 状态位,强制刷新成功 (loop 1 next) 重试一次,失败走 401 报错 - 401 触发刷新时 pi-log-error! 写 [oauth-401] 日志到 ~/.gf/agent/logs/pi-YYYY-MM-DD.log(含 provider、新旧 token 末 4 位、 refresh 结果),便于事后排查 只重试一次、仅 oauth provider、复用 %pi-refresh-oauth-credential! 落盘/清除回调。配套单测覆盖 keyring 钩子返回类型、resolve 强制刷新 不查 expires、401→refresh→200/401 场景。
CI 的 Format Check 对整个 ai 目录跑 gf fmt --check,发现 12 个文件 需格式化(含本任务新增的 pi-refresh-oauth-key-bang-test 与既有 1402 wip 遗留的 storage/credential 等文件)。统一 gf fix + gf fmt 修正。
tui 目录 4 个 1402 wip 遗留文件需格式化:app-login/login-device-code/ worker 及其测试。统一 gf fix + gf fmt 修正。
CI 的 gf check 对整个仓库跑,发现 1402 wip 遗留的违规(本分支带入): - device-flow.scm: 提取 %resolve-interval-ms 与 %apply-slow-down 降嵌套 17→合规 - worker.scm: 压缩版权注释行数 451→450 - 补 4 个缺失 README(device-flow/kimi-coding-oauth/storage/login-device-code) - 补 pi-auth-args-oauth? 与 pi-auth-login-oauth 单测(满足 unit-test 规则)
根因 1:go worker 经 procedure-source 重建 save! 闭包时, pi-credential-* 访问器不在会话活动库列表,save! 抛 unbound 被 %pi-refresh-oauth-credential! 静默吞掉——刷新成功但 auth.json 永远不落盘,每次启动仍用旧 token 反复 401。 - oauth-hook/worker 显式 import (liii pi ai config credential) - save! 失败写 [oauth-refresh] 日志,不再静默吞错(仍返回新 access) 根因 2:refresh-oauth-key 钩子仅启动时注入,同 provider 切模不走 augment、/login 后 reload 只换 api-key,钩子缺失或残留。 - 新增 (liii pi code config oauth-hook):pi-make-refresh-oauth-hook 与 pi-sync-oauth-refresh-hook(按当前 provider 注入/清除钩子) - pi-worker-merge-model(含同 provider 切模)与 %worker-reload-cfg 均重同步;pi-cfg-with-model 委托共享实现 测试:oauth-hook 端到端(stub OAuth 服务)、切模钩子重同步、 save! 失败不阻断回归;真实 go worker 探针验证新 token 正确落盘
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
1402 接入 Kimi Code OAuth2 设备码登录时,§5.5「请求层无需改动」的假设不成立:
api-key在构建 effective-cfg 时一次性 resolve(pi-resolve-provider-key,仅expires到才刷新),请求层循环只读 config 的api-key字段expires之前判 access token 失效返回 401 时,直接以HTTP 401 stream error失败,必须重启或等 expires 到方案
仿照
pi-rotate-api-key!钩子范式新增 401 后 oauth 强制刷新回退(对齐 task 文档devel/1404.stem):pi-oauth-refresh-enabled?/pi-refresh-oauth-key!,钩子返回 string(新 access)/'unauthorized(refresh 失败需重 login)/#f(非 oauth/无凭据)pi-force-refresh-oauth-credential-key(不查 expires 直接刷新)与pi-has-oauth-credential?refresh-oauth-key闭包钩子,落盘/清除复用pi-save-auth-oauth!/pi-remove-auth-provider!refreshed-once?状态位,强制刷新成功(loop 1 next #t)重置 attempt 重试一次,失败走 401 报错pi-log-error!写[oauth-401]行到~/.gf/agent/logs/pi-YYYY-MM-DD.log(含 provider、新旧 token 末 4 位、refresh 结果),不泄露完整 token,便于事后排查边界
refreshed-once?守卫,避免 401→refresh→401 循环)%pi-refresh-oauth-credential!落盘/清除回调,与pi-resolve-oauth-credential-key同源验证
gf fmt --check/gf check全绿kimi-oauth-host-test(环境变量读取问题,与本次改动无关,已确认基线同样失败)测试